package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.ReceptType;
import com.ljxt.dt.domain.SupplierInfo;
import com.ljxt.dt.service.storemanagementservice.AddReceptService;
import com.ljxt.dt.service.storemanagementservice.impl.AddReceptServiceImpl;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.AddFindFoodsVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-12 17:00
 * @copyright 老九学堂
 */
@WebServlet("/addRecept")
public class AddReceptServlet extends HttpServlet {
    private AddReceptService admin = new AddReceptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询入库类型
        List<ReceptType> list = admin.selectAddType();
        //查询供应商
        List<SupplierInfo> supplierInfos = admin.selectSupplier();
       req.setAttribute("type",list);
       req.setAttribute("suplier",supplierInfos);
        ServletUtil.forward(req,resp,"addRecept","storeManagement");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            AddFindFoodsVO findFoodsVO =  ServletUtil.toObj(req,new AddFindFoodsVO());

            //根据条件查找
        Integer  pageIndex = ServletUtil.getQueryInt(req,"pageIndex");
        Integer pageSize = ServletUtil.getQueryInt(req,"pageSize");
        if(pageIndex == 0){
            pageIndex = 1;
        }
        if(pageSize == 0){
            pageSize = 5;
        }

         PageVO pageVO =  admin.selectFoods(findFoodsVO,pageIndex,pageSize);
            pageVO.setPageURL("addRecept");
            pageVO.setSelectObj(findFoodsVO);
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(pageVO);
        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().write(objectMapper.writeValueAsString(jsonResult));

    }
}
